#!/bin/bash


#
# Check for an developer-only FPP_UUID setting
#

UUID=$(grep "^FPP_UUID" /home/fpp/media/settings | cut -f2 -d= | sed -e "s/[^-A-Z0-9]//g")
if [ "$UUID" != "" ]; then
    echo "$UUID"
    exit 0
fi

#
# Try and use CPUInfo -- works on PI and BBB
#
UUID=$(grep -i serial /proc/cpuinfo | awk '{ print $3}')
if [ "$UUID" != "" ] && [ "$UUID" != "0" ]; then
    echo "M1-$UUID"
    exit 0
fi

#
# Try dmidecode
#
UUID=$(sudo dmidecode | grep -i 'serial number' | head -1 | awk '{print $3}')
if [ "$UUID" != "" ] && [ "$UUID" != "0" ]; then
    echo "M2-$UUID"
    exit 0
fi

#
# If all else fails, Genereate one
#
UUIDFILE="/etc/fpp/fpp_uuid"
if [ ! -f $UUIDFILE ]; then
    sudo cp /proc/sys/kernel/random/uuid $UUIDFILE
    sudo chmod 644 $UUIDFILE
fi;

cat $UUIDFILE
